#pragma once

#include <pthread.h>

#define PTHREAD_MUTEX_SUCCESS 0

class ScopedMutexLock {
 public:
  explicit ScopedMutexLock(pthread_mutex_t* mutex) : mutex_(mutex) {
    if (pthread_mutex_lock(mutex_) != PTHREAD_MUTEX_SUCCESS)
      mutex_ = NULL;
  }
  ~ScopedMutexLock() {
    if (mutex_)
      pthread_mutex_unlock(mutex_);
  }
  bool is_valid() const { return mutex_ != NULL; }

 private:
  pthread_mutex_t* mutex_;
};

